unit unDataBase;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, BusinessLogicInterface, unDataCard, cxGraphics, cxControls, cxLookAndFeels, cxLookAndFeelPainters,
  cxContainer, cxEdit, dxSkinsCore, dxSkinBlack, dxSkinBlue, dxSkinCaramel,
  dxSkinCoffee, dxSkinDarkRoom, dxSkinDarkSide, dxSkinFoggy, dxSkinGlassOceans,
  dxSkiniMaginary, dxSkinLilian, dxSkinLiquidSky, dxSkinLondonLiquidSky,
  dxSkinMcSkin, dxSkinMoneyTwins, dxSkinOffice2007Black, dxSkinOffice2007Blue,
  dxSkinOffice2007Green, dxSkinOffice2007Pink, dxSkinOffice2007Silver,
  dxSkinPumpkin, dxSkinSeven, dxSkinSharp, dxSkinSilver, dxSkinSpringTime,
  dxSkinStardust, dxSkinSummer2008, dxSkinsDefaultPainters, dxSkinValentine,
  dxSkinXmas2008Blue, cxGroupBox, DBGridEhGrouping, GridsEh, DBGridEh,
  dxSkinsdxBarPainter, dxBar, cxClasses, DB, ADODB, StdCtrls, ToolWin, ComCtrls;

type
  TfrmDataBase = class(TForm)
    cxGroupBox1: TcxGroupBox;
    dbgMaster: TDBGridEh;
    dxBarManager1: TdxBarManager;
    btnNew: TdxBarLargeButton;
    btnDel: TdxBarLargeButton;
    btnModify: TdxBarLargeButton;
    tblMaster: TADOTable;
    DataSource1: TDataSource;
    dxBarButton1: TdxBarButton;
    QRY: TADOQuery;
    btnSave: TdxBarLargeButton;
    ToolBar1: TToolBar;
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure dbgMasterDblClick(Sender: TObject);
    procedure FormCreate(Sender: TObject);
  private
    { Private declarations }
    FDataCardForm:TfrmDataCard;
    FKeyField:string;
    FKeyFieldSign:string;
    procedure SetDataCaption(const Value:string);
    procedure SetDataFormID(const Value:string);
    procedure SetDataTable(const Value:string);
    procedure SetDataCardForm(const Value:TfrmDataCard);
  public
    { Public declarations }
    function GetKeyValue(KeySign:string):string;
  public
    property DataCaption :string write SetDataCaption;
    property DataFormID :string write SetDataFormID;
    property DataTable :string write SetDataTable;
    property DataCardForm :TfrmDataCard write SetDataCardForm;
    property KeyField: string write FKeyField;
    property KeyFieldSign: string write FKeyFieldSign;
  end;

implementation

{$R *.dfm}

{ TfrmDataBase }

procedure TfrmDataBase.dbgMasterDblClick(Sender: TObject);
begin
  FDataCardForm.ShowModal;
end;

procedure TfrmDataBase.FormClose(Sender: TObject; var Action: TCloseAction);
begin
  FreeAndNil(FDataCardForm);
end;

procedure TfrmDataBase.FormCreate(Sender: TObject);
begin
  QRY.Connection:=TADOConnection(SYSDB.GetDBConnection);
  QRY.SQL.Add('exec GetMaxKeyValue :Key,:Add');
end;

function TfrmDataBase.GetKeyValue(KeySign: string): string;
begin
  QRY.Close;
  QRY.Parameters.ParamByName('Key').Value:=KeySign;
  QRY.Parameters.ParamByName('Add').Value:='Yes';
  QRY.Open;
  result:=QRY.FieldByName('KeyValue').AsString;
end;

procedure TfrmDataBase.SetDataCaption(const Value: string);
begin
  Caption:=Value;
  cxGroupBox1.Caption:=Value;
end;

procedure TfrmDataBase.SetDataCardForm(const Value: TfrmDataCard);
begin
  FDataCardForm:=Value;
  FDataCardForm.DataSet:=tblMaster;
end;

procedure TfrmDataBase.SetDataFormID(const Value: string);
var Q:TADOQuery;
    fname,fchnname,fwidth:TStringList;
    I:integer;
    Col:TColumnEh;
begin
  fname:=TStringList.Create;
  fchnname:=TStringList.Create;
  fwidth:=TStringList.Create;

  Q:=TADOQuery.Create(nil);
  Q.Connection:=TADOConnection(SYSDB.GetDBConnection);
  Q.SQL.Add('select * from DBGridSet where FormID=:ID and DBGridName=:Name');
  Q.Parameters.ParamByName('ID').Value:=Value;
  Q.Parameters.ParamByName('Name').Value:='MASTER';
  Q.Open;
  if not Q.Eof then
  begin
    fname.CommaText:=Q.FieldByName('FieldList').AsString;
    fchnname.CommaText:=Q.FieldByName('FieldCHNName').AsString;
    fwidth.CommaText:=Q.FieldByName('FieldWidth').AsString;
  end;
  dbgMaster.Columns.Clear;
  for I := 0 to fname.Count - 1 do
  begin
    Col:=dbgMaster.Columns.Add;
    Col.FieldName:=fname[I];
    Col.Title.Caption:=fchnname[I];
    Col.Title.Alignment:=taCenter;
    Col.Width:=strtoint(fwidth[I]);
  end;

  FreeAndNil(fname);
  FreeAndNil(fchnname);
  FreeAndNil(fwidth);
  FreeAndNil(Q);
end;

procedure TfrmDataBase.SetDataTable(const Value: string);
begin
  tblMaster.Close;
  tblMaster.Connection:=TADOConnection(SYSDB.GetDBConnection);
  tblMaster.TableName:=Value;
  tblMaster.Open;
end;

end.
